<template>
	<view class="content">
		<view class="top_header">
			<view class="scan_btn" @click="iconClick">
				<uni-icons size="24" color="#fff" type="scan"></uni-icons>
			</view>
			<view class="search_card">
				<view class="left_s">
					<uni-easyinput v-model="keyword" @clear="searchValue" placeholderStyle="font-size:28rpx;"
						:inputBorder="false" placeholder="工单号、机身编码或产品名称查询">
					</uni-easyinput>

					<view class="search_btn" @click="searchValue">
						<uni-icons type="search" size="20" color="#fff"></uni-icons>
					</view>
				</view>
				<view class="phone_btn" @click="onPhone">
					号码查询
				</view>
			</view>
			<view class="search_phone" v-if="phone2">
				<span class="txt">手机号：</span><span class="tel">{{phone2}}</span><uni-icons @click="clearPhone" size="12"
					color="#ccc" type="closeempty"></uni-icons>
			</view>
			<view class="segmented-control">
				<view class="segmented-control__text" :class="{'segmented-control__item--text':current==item.value}"
					v-for="(item,index) in items" :key="index" @click="onClickItem(item.value,index)">
					<span class="text">{{item.title}}</span><span class="red_num"
						v-if="OrderStatusQty&&setOrderNum(item.value)!=0">{{setOrderNum(item.value)}}</span>
				</view>
			</view>
		</view>
		<view class="content-list" :style="{'padding-top':phone2?'284rpx':'200rpx' }">
			<view class="" v-if="PageTotalData[current2].dataList.length>0">
				<view class="c_item" v-for="(item,index) in PageTotalData[current2].dataList" :key="index">
					<p @click="goPage(item.id)">
						<view class="sty_title">
							<view class="workNo">
								{{item.workNo}}
								<image class="btn_c" @click.stop="copy(item.workNo)" src="/static/copy.svg" mode="">
								</image>
							</view>
							<span class="status"
								:class="item.status==1||item.status==2?'status_123':item.status==3?'status_4':item.status==5?'status_6':''">{{item.status==1?'待派单':item.status==2?'待网点确认':item.status==3?'处理中':item.status==4?'已完成':'已取消'}}</span>
						</view>
						<view class="module">{{item.module}}</view>
						<view class="tip_title">
							<view class="tip">
								报单时间：<span class="tip_color">{{item.createTime}}</span>
							</view>
							<view class="tip">
								{{item.status==4?'完成时间':item.status==5?'取消时间':'更新时间'}}：<span
									class="tip_color">{{item.updateTime}}</span>
							</view>
						</view>
					</p>
					<view class="btn_change">
						<button size="mini" class="btn_green" @click="onGreen(item.id)"
							v-if="item.status==4||item.status==5">再次申请</button>
						<template v-if="item.status!=5&&(store.state.userMobileHasLogin||token)">
							<button size="mini" @click="openCancel('cancel',item.id)" v-if="item.status!=4"
								class="btn_again">取消服务</button>
							<button size="mini" @click="openCancel('time',item.id)" class="btn_title"
								v-if="item.status!=4">改约日期</button>
							<button size="mini" v-if="item.status==4" class="btn_title">服务评价</button>
						</template>
					</view>
				</view>
				<view class="no_some" v-if="title">
					{{title}}
				</view>
			</view>
			<view v-if="PageTotalData[current2].dataList.length==0" class="void_goods">
				<image class="img" src="/static/goods.svg"></image>
				<view class="tip">暂无工单</view>
				<p class="tip" v-if="phone2">暂无相关工单</p>
			</view>
		</view>
		<!-- 手机号查询 -->
		<uni-popup ref="popupPhone" type="center">
			<view class="add_main">
				<p>手机号查询</p><uni-icons class="close_this" size="20" @click="popupPhone.close()" color="#999"
					type="closeempty"></uni-icons>
				<p class="phone_tip">仅可查询近三个月的工单</p>
				<uni-easyinput type="number" maxlength="11" placeholderStyle="font-size:28rpx;" v-model="phone"
					placeholder="请输入手机号" />
				<view class="code_re">
					<uni-easyinput type="number" placeholderStyle="font-size:28rpx;" maxlength="6" :clearable="false"
						v-model="codePhone" placeholder="请输入验证码" />
					<view class="code_text" v-if="count==0" @click="getCode">
						获取验证码
					</view>
					<view class="code_text_" v-else>
						({{count}}s)后重新获取
					</view>
				</view>
				<button class="onlogout" @click="queryOrder"><span>查询</span></button>
			</view>
		</uni-popup>
		<!-- 弹出申请方式-->
		<uni-popup ref="showBottom" type="bottom">
			<view class="popup_way">
				<view class="text_top_popup">
					选择类型<uni-icons class="icon" color="#aaa" @click="showBottom.close()" size="22"
						type="closeempty"></uni-icons>
				</view>
				<view class="height_list">
					<uni-list-item v-for="(item,index) in waylist" clickable @click="addWorkor(item)" :key="index">
						<template v-slot:body>
							<view class="way_name">
								<span
									:style="{color:store.state.wayType.id==item.id?'#007cff':''}">{{item.name}}</span><uni-icons
									v-if="store.state.wayType.id==item.id" color="#007cff"
									type="checkmarkempty"></uni-icons>
							</view>
						</template>
					</uni-list-item>
				</view>
			</view>
		</uni-popup>
		<!-- 取消弹窗 -->
		<uni-popup ref="popupCancel" type="dialog">
			<uni-popup-dialog type="warning" cancelText="否" confirmText="是" title="提示" content="确定要取消该服务?"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<TimePopup ref="timeRef" @set-popup="showpopup=$event" :showpopup="showpopup" :workId="workId"></TimePopup>
	</view>

</template>

<script lang="ts" setup>
	import {
		ref, onMounted, reactive
	} from "vue";
	import { onReachBottom, onPullDownRefresh, onUnload, onShow } from '@dcloudio/uni-app'
	import apiOrder from "@/util/api/order.js"
	import apiCode from '@/util/api/getcode.js'
	import TimePopup from '@/components/u-user/TimePopup.vue'
	import store from '@/store/index.js'
	const keyword = ref()//搜索关键词
	const current = ref(0)//当前页面
	const current2 = ref(0)//当前页面
	const timeRef = ref()
	const showpopup = ref(false)
	const items = [{ title: '全部工单', value: 0 }, { title: '服务中', value: 2 }, { title: '完成/取消', value: 3 }, { title: '待评价', value: 4 }]
	const OrderStatusQty = ref(null)//状态数量
	const PageTotalData = ref([{ page: 1, total: 0, dataList: [] }, { page: 1, total: 0, dataList: [] }, { page: 1, total: 0, dataList: [] }, { page: 1, total: 0, dataList: [] }])
	// 0==工单列表 1==待客服确认 2==服务中 3==完成取消 4==待评价
	const onClickItem = (index : number, index2) => {
		if (current2.value == index2) return
		current.value = index//请求的value
		current2.value = index2
		PageTotalData.value[current2.value].page = 1
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 0
		})
		getOrderList()
	}
	onUnload(() => {
		uni.hideLoading()
	})
	const token = ref('')
	onShow(() => {
		token.value = uni.getStorageSync('dd-access-user').token
	})
	const title = ref('没有更多工单')
	const codePhone = ref('')//验证码
	const phone = ref('')//手机号
	const phone2 = ref('')//手机号
	const count = ref(0) //倒计时
	const clear = ref(null)
	const popupPhone = ref()
	const selectHelpOtherCreateOrder = ref(false)
	const popupCancel = ref()
	const workId = ref('')//工单Id
	const isCode = ref(false)
	const showBottom = ref(null) //申请工单弹窗
	let waylist = ref([])
	let orderId = ref(null)
	// 选择申请方式
	const addWorkor = (item) => {
		store.commit('setWayType', {
			id: item.id,
			name: item.name,
			type: item.type//1安装
		})
		showBottom.value.close()
		uni.navigateTo({
			url: "/pages/home/add?id=" + orderId.value
		})
	}
	// 获取报单模块列表
	const getApplyWay = async () => {
		let data = {
			"limit": 0,
			"name": "",
			"page": 0
		}
		uni.showLoading({
			title: "加载中..."
		})
		await apiOrder.getOrderModuleList(data).then(res => {
			if (res.code == 20000) {
				waylist.value = res.data.rows
			}
		})
		uni.hideLoading()
	}
	// 点击再次申请
	const onGreen = async (id) => {
		orderId.value = id
		store.commit('setorderGoodsTypes', [])
		store.commit('setchackFaultList', [])
		store.commit('setchackFaultItem', { id: null })
		store.commit('setWayType', {})
		store.commit('setcheckBrandItem', { id: null })
		store.commit('setcheckClassifyItem', { id: null })
		store.commit('setappointmentTime', null)
		store.commit('setUserOrder', {
			"address": "", //客户详细地址
			"area": "", //地址-县/区
			"city": "", //地址-市
			"helpOtherCreateOrder": false, //	是否代他人建单：0-否，1-是	
			"name": "", //客户姓名
			"otherPhone": "", //其他电话
			"phone": "", //客户手机号
			"province": "", //地址-省
			"town": "", //地址-镇
			"userId": null
		})
		showBottom.value.open()
		await getApplyWay()
	}
	// 获取工单状态数量
	const getOrderStatusNum = async () => {
		uni.showLoading({
			mask: true,
			title: "加载中..."
		})
		let data = {
			"keyword": keyword.value,//工单单号/防伪码/商品名称/型号，支持模糊查询
			"phone": phone2.value,//手机号
			"verifiCode": codePhone.value//验证码
		}
		await apiOrder.getOrderStatusQty(data).then(res => {
			if (res.code == 20000) {
				OrderStatusQty.value = res.data
			}
		})
		uni.hideLoading()
	}
	// 设置对应的工单状态数量 超过99+
	const setOrderNum = (index : any) => {
		switch (index) {
			case 0:
				var num : any = 0
				if (OrderStatusQty.value.totalOrderQty > 100) {
					num = '99+'
				} else {
					num = OrderStatusQty.value.totalOrderQty
				}
				return num
			// case 1:
			// 	var num : any = 0
			// 	if (OrderStatusQty.value.waitServiceSureQty > 100) {
			// 		num = '99+'
			// 	} else {
			// 		num = OrderStatusQty.value.waitServiceSureQty
			// 	}
			// 	return num
			case 2:
				var num : any = 0
				if (OrderStatusQty.value.serviceOrderQty > 100) {
					num = '99+'
				} else {
					num = OrderStatusQty.value.serviceOrderQty
				}
				return num
			case 3:
				var num : any = 0
				if (OrderStatusQty.value.finishOrCancelOrderQty > 100) {
					num = '99+'
				} else {
					num = OrderStatusQty.value.finishOrCancelOrderQty
				}
				return num
			case 4:
				var num : any = 0
				if (OrderStatusQty.value.waitCommentOrderQty > 100) {
					num = '99+'
				} else {
					num = OrderStatusQty.value.waitCommentOrderQty
				}
				return num
		}
	}
	// 预览图片
	const lookImg = (url : any) => {
		uni.previewImage({
			urls: [url],
		})
	}
	// 下拉刷新
	onPullDownRefresh(async () => {
		selectHelpOtherCreateOrder.value = false
		keyword.value = ''
		phone.value = ''
		codePhone.value = ''
		phone2.value = ''
		isCode.value = false
		current.value = 0
		current2.value = 0
		PageTotalData.value = []
		for (let i = 0; i < 4; i++) {
			PageTotalData.value.push({ page: 1, total: 0, dataList: [] })
		}
		await Promise.all([getOrderStatusNum(), getOrderList()]).then(() => {
			uni.stopPullDownRefresh()
		})
	})
	// 复制
	const copy = (val : any) => {
		uni.setClipboardData({
			data: val,
			success: function () {
				//调用方法成功
				uni.showToast({
					title: '已复制',
					icon: 'none'
				})
			},
		})
	}
	// 搜索
	const searchValue = () => {
		getOrderList()
		getOrderStatusNum()
	}
	// 打开手机查询弹窗
	const onPhone = () => {
		popupPhone.value.open()

	}
	// 详情页
	const goPage = (id) => {
		uni.navigateTo({
			url: '/pages/order/detail?id=' + id
		})
	}
	// 清空手机
	const clearPhone = () => {
		phone.value = ''
		codePhone.value = ''
		phone2.value = ''
		isCode.value = false
		selectHelpOtherCreateOrder.value = false
		getOrderList()
		getOrderStatusNum()
	}
	// 手机查询
	const queryOrder = () => {
		if (!/^1[^1^2]\d{9}$/.test(phone.value)) {
			uni.showToast({
				title: '请输入正确的手机号!',
				position: 'top',
				icon: "none"
			});
			return;
		}
		if (!codePhone.value) {
			uni.showToast({
				title: '请输入正确的验证码!',
				position: 'top',
				icon: "none"
			});
			return;
		}
		if (!isCode.value) {
			uni.showToast({
				icon: 'none',
				title: '验证码错误'
			})
			return
		}
		phone2.value = phone.value
		current.value = 0
		current2.value = 0
		PageTotalData.value[current2.value].page = 1
		selectHelpOtherCreateOrder.value = true
		getOrderList()
		getOrderStatusNum()
	}
	// 确认取消工单
	const dialogConfirm = () => {
		if (!workId.value) return
		apiOrder.cancelOrder(workId.value).then(res => {
			if (res.code == 20000) {
				uni.showToast({
					icon: 'success',
					title: res.message
				})
				getOrderList()
				getOrderStatusNum()
			}
		})
	}
	// 关闭取消弹窗
	const dialogClose = () => {
		popupCancel.value.close()
	}
	// 打开取消弹窗
	const openCancel = (type, id) => {
		workId.value = id
		if (type == 'cancel') {
			popupCancel.value.open()
		}
		if (type == 'time') {
			showpopup.value = true
		}
	}
	// 获取验证码
	const getCode = async () => {
		if (!/^1[^1^2]\d{9}$/.test(phone.value)) {
			uni.showToast({
				title: '请输入正确的手机号!',
				position: 'top',
				icon: "none"
			});
			return;
		}
		count.value = 59
		clear.value = setInterval(() => {
			count.value -= 1
			if (count.value == 0) {
				clearInterval(clear.value)
			}
		}, 1000)
		isCode.value = false
		await apiCode.selectCode({
			phone: phone.value
		}).then(res => {
			if (res.code === 20000) {
				isCode.value = true
				uni.showToast({
					title: res.message,
					icon: 'none',
					position: 'top',
				})
			}
		})
	}
	// 调起客户端扫码界面，扫码成功后返回对应的结果。
	const iconClick = () => {
		uni.scanCode({
			scanType: ['qrCode', 'barCode'],//扫码类型
			success: async function (res) {
				keyword.value = res.result
				getOrderList()
				getOrderStatusNum()
			},
			fail: (err) => {
				// #ifdef H5
				uni.showToast({
					icon: 'none',
					title: '设备不支持此功能',
					duration: 3000
				})
				// #endif
			}
		})
	}

	// 获取工单列表
	const getOrderList = async () => {
		uni.showLoading({
			title: "加载中..."
		})
		let data = {
			"keyword": keyword.value,//工单单号/防伪码/商品名称/型号，支持模糊查询
			"limit": 50,//	每页显示数量
			"page": PageTotalData.value[current2.value].page,
			"phone": phone2.value,//手机号
			"selectHelpOtherCreateOrder": selectHelpOtherCreateOrder.value,
			"status": current.value === 0 ? '' : current.value,//	工单状态：1-待服务确认，2-服务中，3-完成/取消，4-待评价
			"verifiCode": codePhone.value//验证码
		}
		await apiOrder.OrderList(data).then(res => {
			if (res.code == 20000) {

				if (PageTotalData.value[current2.value].page > 1) {
					PageTotalData.value[current2.value].dataList = [...PageTotalData.value[current2.value].dataList, ...res.data.rows]
				} else {
					PageTotalData.value[current2.value].dataList = res.data.rows
				}
				PageTotalData.value[current2.value].total = res.data.total
				PageTotalData.value[current2.value].dataList = PageTotalData.value[current2.value].dataList.map(o => {
					o.lookShow = false
					return o
				})
				popupPhone.value.close()
			}
		})
		uni.hideLoading()
	}
	onMounted(() => {
		getOrderStatusNum()
		getOrderList()
	})
	// 触底
	onReachBottom(() => {
		if (PageTotalData.value[current2.value].total > PageTotalData.value[current2.value].dataList.length) {
			PageTotalData.value[current2.value].page = PageTotalData.value[current2.value].page + 1
			title.value = ''
			getOrderStatusNum()
			getOrderList()
		} else {
			title.value = '没有更多工单'
		}
	})
</script>

<style lang="scss" scoped>
	.popup_way {
		height: 60vh;
		border-radius: 20rpx 20rpx 0 0;
		background-color: #fff;
		padding-bottom: 100rpx;

		.text_top_popup {
			text-align: center;
			font-size: 32rpx;
			padding: 20rpx 0;
			font-weight: bold;

			.icon {
				position: absolute;
				right: 40rpx;
				font-weight: 500;
			}
		}

		.height_list {
			height: 100%;
			overflow: auto;

			.way_name {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 28rpx;
			}
		}
	}

	.content {
		background-color: #f6f7fa;
		height: 100vh;

		.top_header {
			background-color: #f6f7fa;
			position: fixed;
			z-index: 3;
			left: 0;
			width: 100%;
			padding: 24rpx 0;

			.scan_btn {
				margin-right: 10rpx;
				position: fixed;
				right: 20rpx;
				bottom: 20vh;
				background-color: rgba(0, 0, 0, 0.6);
				border-radius: 50%;
				padding: 20rpx;
				z-index: 6;
			}

			.search_card {
				display: inline-flex;
				align-items: center;
				width: calc(100% - 48rpx);
				margin: 0 24rpx 24rpx;

				.left_s {
					flex: 1;
					display: inline-flex;
					align-items: center;
					background-color: #f3f3f3;
					border-radius: 40rpx;

					:deep(.uni-easyinput__content) {
						height: 70rpx;
						line-height: 70rpx;
						border-radius: 40rpx 0 0 40rpx;
						background-color: #fff !important;
					}

					.search_btn {
						padding: 0 20rpx;
						height: 69rpx;
						background-color: #007cff;
						border-radius: 0 40rpx 40rpx 0;
						display: flex;
						justify-content: center;
						align-items: center;

					}
				}

				.phone_btn {
					font-size: 24rpx;
					color: #007cff;
					margin-left: 10rpx;
				}
			}

			.search_phone {
				font-size: 28rpx;
				margin: 0rpx 24rpx 10rpx;
				background-color: #f3f3f3;
				display: inline-block;
				padding: 12rpx;
				border-radius: 20rpx;

				.tel {
					color: #007cff;
					margin-right: 12rpx;
				}
			}

			:deep(.segmented-control) {
				border-bottom: 1rpx solid #eee;
				box-sizing: border-box;
				display: flex;
				justify-content: space-around;
				padding: 22rpx 20rpx 0;
				background-color: #fff;

				.segmented-control__text {
					font-size: 28rpx !important;
					font-weight: bold;
					border-bottom: 4rpx solid transparent;
					padding: 10rpx;
					position: relative;
					z-index: 5;

					.text {
						background-color: #fff;
						position: relative;
						z-index: 1;
						border-radius: 10rpx;
						padding: 0 4rpx;
					}

					.red_num {
						border: 4rpx solid #FF5733;
						border-radius: 12rpx;
						min-width: 30rpx;
						height: 30rpx;
						line-height: 30rpx;
						position: absolute;
						top: -17rpx;
						right: -10rpx;
						text-align: center;
						color: #FF5733;
						font-size: 20rpx;
						transform: scale(.9);
						z-index: -1;
						padding: 0 2rpx;
					}
				}

				.segmented-control__item--text {
					border-bottom: 4rpx solid #007cff;
					padding: 10rpx;
					color: #007cff;
					transition: all .5s;
				}
			}
		}

		.content-list {
			background-color: #f6f7fa;
			padding-bottom: 200rpx;

			.no_some {
				font-size: 24rpx;
				color: #ccc;
				text-align: center;
				width: 100%;
			}

			.c_item {
				background-color: #fff;
				margin: 24rpx;
				padding: 24rpx 20rpx 12rpx;
				border-radius: 20rpx;
				// position: relative;

				.sty_title {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.status {
						font-size: 24rpx;
						background-color: #ecf5ff;
						color: #007cff;
						padding: 8rpx;
						border-radius: 8rpx;
					}

					.status_123 {
						color: #e6a23c;
						background-color: #fdf6ec;

					}

					.status_4 {
						color: #00c0c0;
						background-color: #ebf6f6;

					}

					.status_6 {
						color: #909399;
						background-color: #f4f4f5;

					}

					.workNo {
						font-size: 28rpx;
						// font-weight: bold;
						// padding: 20rpx 0 24rpx;
						display: flex;
						align-items: center;

						.btn_c {
							height: 30rpx;
							line-height: 30rpx;
							width: 24rpx;
							margin-left: 24rpx;
						}
					}

				}

				.module {
					font-size: 24rpx;
					// color: #666;
					font-weight: bold;
					// margin-left: 8rpx;
					padding: 12rpx 0 16rpx;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					word-wrap: break-word;
					word-break: break-all;

				}


				.tip_title {
					font-size: 24rpx;
					color: #777;

					.tip {
						padding-bottom: 6rpx;

						.tip_color {
							color: #000;
						}
					}
				}


				.look_some {
					text-align: center;
				}

				.btn_change {
					text-align: right;

					uni-button:after {
						border: 0rpx;
					}

					.btn_green {
						background-color: #fff;
						padding: 0 12rpx;
						border-radius: 10rpx;
						color: #00c0c0;
						font-size: 24rpx;
						margin-left: 24rpx;
						border: 1rpx solid #00c0c0;
					}

					.btn_again {
						background-color: #fff;
						padding: 0 12rpx;
						border-radius: 10rpx;
						color: #666;
						font-size: 24rpx;
						margin-left: 24rpx;
						border: 1rpx solid #999;
					}

					.btn_title {
						background-color: #fff;
						padding: 0 12rpx;
						border-radius: 10rpx;
						color: #007cff;
						font-size: 24rpx;
						margin-left: 24rpx;
						border: 1rpx solid #007cff;
						box-shadow: 0;
					}
				}
			}

			.void_goods {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				width: 100%;
				height: 60vh;

				.img {
					width: 60%;
					height: 250rpx;
				}

				.tip {
					color: #aaa;
					font-size: 28rpx;
				}
			}
		}

		.add_main {
			background-color: #fff;
			border-radius: 20rpx;
			width: 80vw;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			padding: 24rpx 40rpx;
			box-sizing: border-box;
			position: relative;

			.phone_tip {
				font-size: 24rpx;
				color: rgba(255, 0, 0, 1);
				padding-top: 12rpx;
			}

			.close_this {
				position: absolute;
				right: 24rpx;
				top: 24rpx;
			}

			.code_re {
				position: relative;
				width: 100%;

				.code_text {
					height: 30rpx;
					line-height: 30rpx;
					font-size: 28rpx;
					color: #007cff;
					position: absolute;
					top: 55%;
					right: 30rpx;
					z-index: 2;
				}

				.code_text_ {
					height: 30rpx;
					font-size: 28rpx;
					line-height: 30rpx;
					color: #999;
					position: absolute;
					top: 55%;
					right: 30rpx;
				}
			}

			.onlogout {
				font-size: 28rpx;
				margin: 30rpx 20rpx 0;
				height: 80rpx;
				border-radius: 50rpx;
				line-height: 80rpx;
				width: 100%;
				color: #fff;
				background-color: #007cff;
			}

			:deep(.is-input-border) {
				border-radius: 20rpx !important;
				background-color: rgba(243, 244, 248, 1) !important;
				padding: 10rpx;
				margin-top: 40rpx;
				border: none;
				height: 80rpx !important;
				line-height: 80rpx;
				font-size: 28rpx;
			}
		}
	}
</style>